package com.gagakuai.mapping;

import com.gagakuai.reflect.MetaObject;

import java.util.List;
import java.util.Map;

/*
 * @program: common-starters
 * @description: 绑定的sql的抽象
 * @author: houhong
 * @create: 2023-06-29 23:46
 */
public class BoundSql {


    /*
     * Mapper配置解析后的sql语句
     */
    private final String sql;
    /*
     * Mapper参数映射信息
     */
    private final List<ParameterMapping> parameterMappings;

    /*
     * Mapper参数对象
     */
    private final Object parameterObject;
    /*
     * 额外参数信息，包括<bind>标签绑定的参数，内置参数
     */
    private final Map<String, Object> additionalParameters;

    /*
     * 参数对象对应的MetaObject对象
     */
    private final MetaObject metaParameters;


    public BoundSql(String sql, List<ParameterMapping> parameterMappings, Object parameterObject, Map<String, Object> additionalParameters, MetaObject metaParameters) {
        this.sql = sql;
        this.parameterMappings = parameterMappings;
        this.parameterObject = parameterObject;
        this.additionalParameters = additionalParameters;
        this.metaParameters = metaParameters;
    }


    public String getSql() {
        return sql;
    }

    public List<ParameterMapping> getParameterMappings() {
        return parameterMappings;
    }

    public Object getParameterObject() {
        return parameterObject;
    }

    public boolean hasAdditionalParameter(String name) {
        String paramName = new PropertyTokenizer(name).getName();
        return additionalParameters.containsKey(paramName);
    }

    public void setAdditionalParameter(String name, Object value) {
        metaParameters.setValue(name, value);
    }

    public Object getAdditionalParameter(String name) {
        return metaParameters.getValue(name);
    }
}